package com.life.wang.service.impl;


import com.life.wang.mapper.TbUserMapper;
import com.life.wang.pojo.TbUser;
import com.life.wang.pojo.TbUserExample;
import com.life.wang.pojo.vo.User;
import com.life.wang.service.UserService;
import com.life.wang.util.FastDFSClient;
import com.life.wang.util.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private TbUserMapper userMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private FastDFSClient fastDFSClient;
    @Autowired
    private Environment env;

    @Override
    public List<TbUser> findAll() {
        return userMapper.selectByExample(null);
    }

    @Override
    public User login(String username, String password) {
        if(StringUtils.isNotBlank(username)&&StringUtils.isNotBlank(password)){
            TbUserExample example = new TbUserExample();
            TbUserExample.Criteria criteria = example.createCriteria();
            //查询条件
            criteria.andUsernameEqualTo(username);

            List<TbUser> userList = userMapper.selectByExample(example);
            if(!CollectionUtils.isEmpty(userList)&&1==userList.size()){
                //对密码进行校验
                String encodingPassword = DigestUtils.md5DigestAsHex(password.getBytes());
                if(encodingPassword.equals(userList.get(0).getPassword())){
                    User user = new User();
                    BeanUtils.copyProperties(userList.get(0),user);
                    return user;
                }
            }
        }
        return null;
    }

    @Override
    public void register(TbUser user) {
        try{
            //判断用户名是否重复
            TbUserExample example = new TbUserExample();
            TbUserExample.Criteria criteria = example.createCriteria();

            criteria.andUsernameEqualTo(user.getUsername());

            List<TbUser> userList = userMapper.selectByExample(example);
            if(!CollectionUtils.isEmpty(userList))
                throw new RuntimeException("用户名已存在！");

            //使用雪花算法生成唯一id
            user.setId(idWorker.nextId());
            //密码加密
            user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
            user.setNickname(user.getUsername());
            user.setPicSmall("");
            user.setPicNormal("");
            user.setCreatetime(new Date());
            userMapper.insert(user);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("注册失败");
        }
    }

    @Override
    public User upload(MultipartFile file, String userid) {

        try {
            //返回图片在FastDFS中的路径，但是不带http://ip/..
            String url = fastDFSClient.uploadFace(file);
            //在FastDFS上传的时候，会自动生成一个缩略图  文件名_150x150.后缀
            String[] fileNameList = url.split("\\.");
            String fileName = fileNameList[0];
            String ext = fileNameList[1];
            String picSmallUrl = fileName + "_150x150."+ext;

            String perfix = env.getProperty("fdfs.httpurl");
            TbUser tbUser = userMapper.selectByPrimaryKey(userid);
            tbUser.setPicNormal(perfix+url);//大图片
            tbUser.setPicSmall(perfix+picSmallUrl);//缩略图
            userMapper.updateByPrimaryKey(tbUser);

            //将用户信息拷贝到user返回到controller
            User user = new User();
            BeanUtils.copyProperties(tbUser,user);
            return user;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
